import { createRouter, createWebHistory } from 'vue-router'
import { getToken, getUserInfo } from '@/utils/auth'
import { ElMessage } from 'element-plus'

// 路由配置
const routes = [
	{
		path: '/',
		name: 'Login',
		component: () => import('../views/Login.vue'),
	},
	{
		path: '/admin',
		component: () => import('../layouts/AdminLayout.vue'),
		redirect: '/admin/students',
		meta: { requireAuth: true, role: 'admin' },
		children: [
			{
				path: 'students',
				name: 'AdminStudents',
				component: () => import('../views/admin/Students.vue'),
				meta: { title: '学生管理' },
			},
			{
				path: 'sport-items',
				name: 'SportItemManagement',
				component: () => import('../views/admin/SportItems.vue'),
				meta: { title: '体测项目管理' },
			},
			{
				path: 'scores',
				name: 'ScoreManagement',
				component: () => import('../views/admin/Scores.vue'),
				meta: { title: '成绩管理' },
			},
			{
				path: 'statistics',
				name: 'Statistics',
				component: () => import('../views/admin/Statistics.vue'),
				meta: { title: '统计分析' },
			},
			{
				path: 'announcements',
				name: 'AnnouncementManagement',
				component: () => import('../views/admin/Announcements.vue'),
				meta: { title: '公告管理' },
			},
			{
				path: 'profile',
				name: 'AdminProfile',
				component: () => import('../views/admin/Profile.vue'),
				meta: { title: '个人中心' },
			},
		],
	},
	{
		path: '/student',
		component: () => import('../layouts/StudentLayout.vue'),
		redirect: '/student/announcements',
		meta: { requireAuth: true, role: 'student' },
		children: [
			{
				path: 'announcements',
				name: 'StudentAnnouncements',
				component: () => import('../views/student/Announcements.vue'),
				meta: { title: '公告栏' },
			},
			{
				path: 'announcement/:id',
				name: 'AnnouncementDetail',
				component: () => import('../views/student/AnnouncementDetail.vue'),
				meta: { title: '公告详情' },
			},
			{
				path: 'scores',
				name: 'StudentScores',
				component: () => import('../views/student/Scores.vue'),
				meta: { title: '我的成绩' },
			},
			{
				path: 'profile',
				name: 'StudentProfile',
				component: () => import('../views/student/Profile.vue'),
				meta: { title: '个人中心' },
			},
		],
	},
	{
		// 捕获所有未匹配的路由
		path: '/:pathMatch(.*)*',
		redirect: '/',
	},
]

// 创建路由实例
const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes,
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
	// 设置页面标题
	document.title = to.meta.title
		? `${to.meta.title} - 智能体测管理系统`
		: '智能体测管理系统'

	// 判断该路由是否需要登录权限
	if (to.matched.some(record => record.meta.requireAuth)) {
		// 检查当前会话状态
		const token = getToken()

		// 没有token，重定向到登录页
		if (!token) {
			ElMessage.error('请先登录')
			next({ path: '/' })
			return
		}

		// 获取当前会话的用户信息
		const userInfo = getUserInfo()
		if (!userInfo) {
			ElMessage.error('用户信息获取失败，请重新登录')
			next({ path: '/' })
			return
		}

		// 检查角色权限
		const userRole = userInfo.type
		if (
			to.matched.some(
				record => record.meta.role && record.meta.role !== userRole
			)
		) {
			if (userRole === 'admin') {
				next({ path: '/admin/dashboard' })
			} else if (userRole === 'student') {
				next({ path: '/student/announcements' })
			} else {
				next({ path: '/' })
			}
			return
		}

		next()
	} else {
		next()
	}
})

export default router
